package com.itgaohe.controller;

import com.itgaohe.dao.entity.Voucher;
import com.itgaohe.service.VoucherService;
import com.itgaohe.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 优惠券控制层
 */

@Slf4j
@RestController
@RequestMapping("/voucher")
@RequiredArgsConstructor
@Tag(name = "优惠券管理")
public class VoucherController {
    private final VoucherService voucherService;
    /**
     * 根据商户id查询优惠券
     * @param shopId 商户id
     * @return
     */
    @Operation(summary = "根据商户id查询优惠券")
    @GetMapping("/list/{shopId}")
    public Result getVoucherByShopId(@PathVariable Long shopId) {
        Result vouchers = voucherService.getVoucherByShopId(shopId);
        return vouchers;
//        long count = voucherList.stream().count();
//        if(count != 0){
//            return Result.ok(voucherList,count);
//        }
//        return Result.fail("该商户暂无优惠券！");
    }

    /**
     * 创建优惠券
     * @param voucher 优惠券字段
     * @return
     */
    @Operation(summary = "创建优惠券")
    @PostMapping("/add")
    public Result addVoucher(@RequestBody Voucher voucher) {
        boolean save = voucherService.save(voucher);
        if(save){
            return Result.ok(voucher);
        }
        return Result.fail("创建失败！");
    }





}
